<template>
<div :style='{"border":"1px solid #9ce3b5","padding":"20px","boxShadow":"0px 2px 6px #ccc,inset 0px 0px 600px 0px #e0f8e8","margin":"20px auto 0","borderRadius":"16px","background":"#fff","width":"1200px","position":"relative"}'>
    <el-form
      class="add-update-preview"
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-width="80px"
    >
          <el-form-item :style='{"border":"0px solid #dfdfdf","padding":"10px","boxShadow":"0px 0px 0px #eee","margin":"0 0 8px 0","borderRadius":"8px","background":"none"}' label="党员账号" prop="dangyuanzhanghao">
            <el-input v-model="ruleForm.dangyuanzhanghao" 
                placeholder="党员账号" clearable ></el-input>
          </el-form-item>
          <el-form-item :style='{"border":"0px solid #dfdfdf","padding":"10px","boxShadow":"0px 0px 0px #eee","margin":"0 0 8px 0","borderRadius":"8px","background":"none"}' label="党员姓名" prop="dangyuanxingming">
            <el-input v-model="ruleForm.dangyuanxingming" 
                placeholder="党员姓名" clearable ></el-input>
          </el-form-item>
          <el-form-item :style='{"border":"0px solid #dfdfdf","padding":"10px","boxShadow":"0px 0px 0px #eee","margin":"0 0 8px 0","borderRadius":"8px","background":"none"}' label="投票主题" prop="toupiaozhuti">
            <el-input v-model="ruleForm.toupiaozhuti" 
                placeholder="投票主题" clearable ></el-input>
          </el-form-item>
          <el-form-item :style='{"border":"0px solid #dfdfdf","padding":"10px","boxShadow":"0px 0px 0px #eee","margin":"0 0 8px 0","borderRadius":"8px","background":"none"}'  label="选项a" prop="xuanxianga">
            <el-select v-model="ruleForm.xuanxianga" placeholder="请选择选项a"  >
              <el-option
                  v-for="(item,index) in xuanxiangaOptions"
                  :key="index"
                  :label="item"
                  :value="item">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item :style='{"border":"0px solid #dfdfdf","padding":"10px","boxShadow":"0px 0px 0px #eee","margin":"0 0 8px 0","borderRadius":"8px","background":"none"}'  label="选项b" prop="xuanxiangb">
            <el-select v-model="ruleForm.xuanxiangb" placeholder="请选择选项b"  >
              <el-option
                  v-for="(item,index) in xuanxiangbOptions"
                  :key="index"
                  :label="item"
                  :value="item">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item :style='{"border":"0px solid #dfdfdf","padding":"10px","boxShadow":"0px 0px 0px #eee","margin":"0 0 8px 0","borderRadius":"8px","background":"none"}'  label="选项c" prop="xuanxiangc">
            <el-select v-model="ruleForm.xuanxiangc" placeholder="请选择选项c"  >
              <el-option
                  v-for="(item,index) in xuanxiangcOptions"
                  :key="index"
                  :label="item"
                  :value="item">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item :style='{"border":"0px solid #dfdfdf","padding":"10px","boxShadow":"0px 0px 0px #eee","margin":"0 0 8px 0","borderRadius":"8px","background":"none"}'  label="选项d" prop="xuanxiangd">
            <el-select v-model="ruleForm.xuanxiangd" placeholder="请选择选项d"  >
              <el-option
                  v-for="(item,index) in xuanxiangdOptions"
                  :key="index"
                  :label="item"
                  :value="item">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item :style='{"border":"0px solid #dfdfdf","padding":"10px","boxShadow":"0px 0px 0px #eee","margin":"0 0 8px 0","borderRadius":"8px","background":"none"}' label="投票时间" prop="toupiaoshijian" >
              <el-date-picker
                  value-format="yyyy-MM-dd HH:mm:ss"
                  v-model="ruleForm.toupiaoshijian" 
                  type="datetime"
                  placeholder="投票时间">
              </el-date-picker>
          </el-form-item>

      <el-form-item :style='{"padding":"0","margin":"20px 0 20px 0"}'>
        <el-button :style='{"border":"0","cursor":"pointer","padding":"0","boxShadow":"1px 2px 3px #aaa","margin":"0 20px 0 0","color":"#fff","outline":"none","borderRadius":"4px","background":"linear-gradient(180deg, rgba(17,112,210,1) 0%, rgba(8,179,68,1) 100%)","width":"128px","lineHeight":"40px","fontSize":"14px","height":"40px"}'  type="primary" @click="onSubmit">提交</el-button>
        <el-button :style='{"border":"1px solid #bbb","cursor":"pointer","padding":"0","boxShadow":"1px 2px 6px #ccc","margin":"0","color":"#999","outline":"none","borderRadius":"4px","background":"#fff","width":"128px","lineHeight":"40px","fontSize":"14px","height":"40px"}' @click="back()">返回</el-button>
      </el-form-item>
    </el-form>
</div>
</template>

<script>
  export default {
    data() {
      return {
        id: '',
        baseUrl: '',
        ro:{
            dangyuanzhanghao : false,
            dangyuanxingming : false,
            toupiaozhuti : false,
            xuanxianga : false,
            xuanxiangb : false,
            xuanxiangc : false,
            xuanxiangd : false,
            toupiaoshijian : false,
            crossuserid : false,
            crossrefid : false,
        },
        type: '',
        userTableName: localStorage.getItem('UserTableName'),
        ruleForm: {
          dangyuanzhanghao: '',
          dangyuanxingming: '',
          toupiaozhuti: '',
          xuanxianga: '',
          xuanxiangb: '',
          xuanxiangc: '',
          xuanxiangd: '',
          toupiaoshijian: '',
          crossuserid: '',
          crossrefid: '',
        },
        xuanxiangaOptions: [],
        xuanxiangbOptions: [],
        xuanxiangcOptions: [],
        xuanxiangdOptions: [],
        rules: {
          dangyuanzhanghao: [
          ],
          dangyuanxingming: [
          ],
          toupiaozhuti: [
          ],
          xuanxianga: [
            { validator: this.$validate.isIntNumer, trigger: 'blur' },
          ],
          xuanxiangb: [
            { validator: this.$validate.isIntNumer, trigger: 'blur' },
          ],
          xuanxiangc: [
            { validator: this.$validate.isIntNumer, trigger: 'blur' },
          ],
          xuanxiangd: [
            { validator: this.$validate.isIntNumer, trigger: 'blur' },
          ],
          toupiaoshijian: [
          ],
          crossuserid: [
          ],
          crossrefid: [
          ],
        },
      };
    },
    computed: {



    },
    created() {
	  //this.bg();
      let type = this.$route.query.type ? this.$route.query.type : '';
      this.init(type);
      this.baseUrl = this.$config.baseUrl;
      this.ruleForm.toupiaoshijian = this.getCurDateTime()
    },
    methods: {
      getMakeZero(s) {
          return s < 10 ? '0' + s : s;
      },
      // 下载
      download(file){
        window.open(`${file}`)
      },
      // 初始化
      init(type) {
        this.type = type;
        if(type=='cross'){
          var obj = JSON.parse(localStorage.getItem('crossObj'));
          for (var o in obj){
            if(o=='dangyuanzhanghao'){
              this.ruleForm.dangyuanzhanghao = obj[o];
              this.ro.dangyuanzhanghao = true;
              continue;
            }
            if(o=='dangyuanxingming'){
              this.ruleForm.dangyuanxingming = obj[o];
              this.ro.dangyuanxingming = true;
              continue;
            }
            if(o=='toupiaozhuti'){
              this.ruleForm.toupiaozhuti = obj[o];
              this.ro.toupiaozhuti = true;
              continue;
            }
            if(o=='xuanxianga'){
              this.ruleForm.xuanxianga = obj[o];
              this.ro.xuanxianga = true;
              continue;
            }
            if(o=='xuanxiangb'){
              this.ruleForm.xuanxiangb = obj[o];
              this.ro.xuanxiangb = true;
              continue;
            }
            if(o=='xuanxiangc'){
              this.ruleForm.xuanxiangc = obj[o];
              this.ro.xuanxiangc = true;
              continue;
            }
            if(o=='xuanxiangd'){
              this.ruleForm.xuanxiangd = obj[o];
              this.ro.xuanxiangd = true;
              continue;
            }
            if(o=='toupiaoshijian'){
              this.ruleForm.toupiaoshijian = obj[o];
              this.ro.toupiaoshijian = true;
              continue;
            }
            if(o=='crossuserid'){
              this.ruleForm.crossuserid = obj[o];
              this.ro.crossuserid = true;
              continue;
            }
            if(o=='crossrefid'){
              this.ruleForm.crossrefid = obj[o];
              this.ro.crossrefid = true;
              continue;
            }
          }
          this.ruleForm.xuanxianga = 0
          this.ruleForm.xuanxiangb = 0
          this.ruleForm.xuanxiangc = 0
          this.ruleForm.xuanxiangd = 0
        }
        // 获取用户信息
        this.$http.get(this.userTableName + '/session', {emulateJSON: true}).then(res => {
          if (res.data.code == 0) {
            var json = res.data.data;
            if((json.dangyuanzhanghao!=''&&json.dangyuanzhanghao) || json.dangyuanzhanghao==0){
                this.ruleForm.dangyuanzhanghao = json.dangyuanzhanghao
            }
            if((json.dangyuanxingming!=''&&json.dangyuanxingming) || json.dangyuanxingming==0){
                this.ruleForm.dangyuanxingming = json.dangyuanxingming
            }
          }
        });
        this.xuanxiangaOptions = "1".split(',')
        this.xuanxiangbOptions = "1".split(',')
        this.xuanxiangcOptions = "1".split(',')
        this.xuanxiangdOptions = "1".split(',')
      },

    // 多级联动参数
      // 多级联动参数
      info(id) {
        this.$http.get('toupiaoxinxi/detail/${id}', {emulateJSON: true}).then(res => {
          if (res.data.code == 0) {
            this.ruleForm = res.data.data;
          }
        });
      },
      // 提交
      onSubmit() {

        var obj = JSON.parse(localStorage.getItem('crossObj'));
        var table = localStorage.getItem('crossTable');
        obj.xuanxianga = parseFloat(obj.xuanxianga) + parseFloat(this.ruleForm.xuanxianga)

        //this.$http.post(table+`/update`, obj).then(res => {});
        var obj = JSON.parse(localStorage.getItem('crossObj'));
        var table = localStorage.getItem('crossTable');
        obj.xuanxiangb = parseFloat(obj.xuanxiangb) + parseFloat(this.ruleForm.xuanxiangb)

        //this.$http.post(table+`/update`, obj).then(res => {});
        var obj = JSON.parse(localStorage.getItem('crossObj'));
        var table = localStorage.getItem('crossTable');
        obj.xuanxiangc = parseFloat(obj.xuanxiangc) + parseFloat(this.ruleForm.xuanxiangc)

        //this.$http.post(table+`/update`, obj).then(res => {});
        var obj = JSON.parse(localStorage.getItem('crossObj'));
        var table = localStorage.getItem('crossTable');
        obj.xuanxiangd = parseFloat(obj.xuanxiangd) + parseFloat(this.ruleForm.xuanxiangd)

        //this.$http.post(table+`/update`, obj).then(res => {});
        //更新跨表属性
        var crossuserid;
        var crossrefid;
        var crossoptnum;
        this.$refs["ruleForm"].validate(valid => {
          if(valid) {
            if(this.type=='cross'){
                 var statusColumnName = localStorage.getItem('statusColumnName');
                 var statusColumnValue = localStorage.getItem('statusColumnValue');
                 if(statusColumnName && statusColumnName!='') {
                     var obj = JSON.parse(localStorage.getItem('crossObj'));
                     if(!statusColumnName.startsWith("[")) {
                         for (var o in obj){
                             if(o==statusColumnName){
                                 obj[o] = statusColumnValue;
                             }
                         }
                         var table = localStorage.getItem('crossTable');
                         this.$http.post(table+'/update', obj).then(res => {});
                     } else {
                            crossuserid=Number(localStorage.getItem('userid'));
                            crossrefid=obj['id'];
                            crossoptnum=localStorage.getItem('statusColumnName');
                            crossoptnum=crossoptnum.replace(/\[/,"").replace(/\]/,"");
                     }
                 }
            }
            if(crossrefid && crossuserid) {
                 this.ruleForm.crossuserid=crossuserid;
                 this.ruleForm.crossrefid=crossrefid;
                 var params = {
                     page: 1,
                     limit: 10,
                     crossuserid:crossuserid,
                     crossrefid:crossrefid,
                 }
                 this.$http.get('toupiaoxinxi/list', {
                  params: params
                 }).then(res => {
                     if(res.data.data.total>=crossoptnum) {
                         this.$message({
                          message: localStorage.getItem('tips'),
                          type: 'success',
                          duration: 1500,
                         });
                          return false;
                     } else {
                         // 跨表计算
                          var obj = JSON.parse(localStorage.getItem('crossObj'));
                          var table = localStorage.getItem('crossTable');

                          obj.xuanxianga = parseFloat(obj.xuanxianga) + parseFloat(this.ruleForm.xuanxianga)
                          obj.xuanxiangb = parseFloat(obj.xuanxiangb) + parseFloat(this.ruleForm.xuanxiangb)
                          obj.xuanxiangc = parseFloat(obj.xuanxiangc) + parseFloat(this.ruleForm.xuanxiangc)
                          obj.xuanxiangd = parseFloat(obj.xuanxiangd) + parseFloat(this.ruleForm.xuanxiangd)

                          this.$http.post(table+`/update`,obj).then(res => {});
                          this.$http.post('toupiaoxinxi/add', this.ruleForm).then(res => {
                              if (res.data.code == 0) {
                                  this.$message({
                                      message: '操作成功',
                                      type: 'success',
                                      duration: 1500,
                                      onClose: () => {
                                          this.$router.go(-1);
                                      }
                                  });
                              } else {
                                  this.$message({
                                      message: res.data.msg,
                                      type: 'error',
                                      duration: 1500
                                  });
                              }
                          });
                     }
                 });
             } else {
                  var obj = JSON.parse(localStorage.getItem('crossObj'));
                  var table = localStorage.getItem('crossTable');

                  obj.xuanxianga = parseFloat(obj.xuanxianga) + parseFloat(this.ruleForm.xuanxianga)
                  obj.xuanxiangb = parseFloat(obj.xuanxiangb) + parseFloat(this.ruleForm.xuanxiangb)
                  obj.xuanxiangc = parseFloat(obj.xuanxiangc) + parseFloat(this.ruleForm.xuanxiangc)
                  obj.xuanxiangd = parseFloat(obj.xuanxiangd) + parseFloat(this.ruleForm.xuanxiangd)

                  this.$http.post(table+`/update`,obj).then(res => {});
                  this.$http.post('toupiaoxinxi/add', this.ruleForm).then(res => {
                     if (res.data.code == 0) {
                          this.$message({
                              message: '操作成功',
                              type: 'success',
                              duration: 1500,
                              onClose: () => {
                                  this.$router.go(-1);
                              }
                          });
                      } else {
                          this.$message({
                              message: res.data.msg,
                              type: 'error',
                              duration: 1500
                          });
                      }
                  });
             }
          }
        });
      },
      // 获取uuid
      getUUID () {
        return new Date().getTime();
      },
      // 返回
      back() {
        this.$router.go(-1);
      },
    }
  };
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
	.el-date-editor.el-input {
		width: auto;
	}
	
	.add-update-preview .el-form-item /deep/ .el-form-item__label {
	  padding: 0 10px 0 0;
	  color: #666;
	  font-weight: 500;
	  width: 80px;
	  font-size: 14px;
	  line-height: 40px;
	  text-align: right;
	}
	
	.add-update-preview .el-form-item /deep/ .el-form-item__content {
	  margin-left: 80px;
	}
	
	.add-update-preview .el-input /deep/ .el-input__inner {
	  border: 1px solid #ccc;
	  border-radius: 4px;
	  padding: 0 12px;
	  outline: none;
	  color: #999;
	  background: #fff;
	  width: 400px;
	  font-size: 14px;
	  height: 40px;
	}
	
	.add-update-preview .el-select /deep/ .el-input__inner {
	  border: 1px solid #ccc;
	  border-radius: 4px;
	  padding: 0 10px;
	  outline: none;
	  color: #999;
	  background: #fff;
	  width: 200px;
	  font-size: 14px;
	  height: 40px;
	}
	
	.add-update-preview .el-date-editor /deep/ .el-input__inner {
	  border: 1px solid #ccc;
	  border-radius: 4px;
	  padding: 0 10px 0 30px;
	  outline: none;
	  color: #999;
	  background: #fff;
	  width: 200px;
	  font-size: 14px;
	  height: 40px;
	}
	
	.add-update-preview /deep/ .el-upload--picture-card {
		background: transparent;
		border: 0;
		border-radius: 0;
		width: auto;
		height: auto;
		line-height: initial;
		vertical-align: middle;
	}
	
	.add-update-preview /deep/ .upload .upload-img {
	  border: 1px solid #ccc;
	  cursor: pointer;
	  border-radius: 6px;
	  color: #999;
	  background: #fff;
	  width: 200px;
	  font-size: 32px;
	  line-height: 120px;
	  text-align: center;
	  height: auto;
	}
	
	.add-update-preview /deep/ .el-upload-list .el-upload-list__item {
	  border: 1px solid #ccc;
	  cursor: pointer;
	  border-radius: 6px;
	  color: #999;
	  background: #fff;
	  width: 200px;
	  font-size: 32px;
	  line-height: 120px;
	  text-align: center;
	  height: auto;
	}
	
	.add-update-preview /deep/ .el-upload .el-icon-plus {
	  border: 1px solid #ccc;
	  cursor: pointer;
	  border-radius: 6px;
	  color: #999;
	  background: #fff;
	  width: 200px;
	  font-size: 32px;
	  line-height: 120px;
	  text-align: center;
	  height: auto;
	}
	
	.add-update-preview .el-textarea /deep/ .el-textarea__inner {
	  border: 1px solid #ccc;
	  border-radius: 4px;
	  padding: 12px;
	  outline: none;
	  color: #999;
	  background: #fff;
	  width: 400px;
	  font-size: 14px;
	  min-height: 120px;
	}
</style>
